package com.elcl.adapter;

import android.util.SparseArray;
import android.view.View;

public class AbViewHolder {

	/**
	 *  adapter中复写ViewHolder，调用方法为
	 *  if (convertView == null) {
     *  convertView = LayoutInflater.from(context)}
     *  ImageView bananaView = AbViewHolder.get(convertView, R.id.banana);
     *  SparseArray是优化的HashMap
     *  T泛型的使用，是规定返回值的时候使用
	 */
	@SuppressWarnings("unchecked")
	public static View getView(View view, int id) {
		SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
		if (viewHolder == null) {
			viewHolder = new SparseArray<View>();
			view.setTag(viewHolder);
		}
		View childView = viewHolder.get(id);
		if (childView == null) {
			childView = view.findViewById(id);
			viewHolder.put(id, childView);
		}
		return childView;
	}
}
